$(function() {

     var password = $( "#password" ),
    repassword = $( "#repassword" ),
    allFields = $( [] ).add( password ).add( repassword ),
    tips = $( ".validateTips" );

    function updateTips( t ) {
        tips
        .text( t )
        .addClass( "ui-state-highlight" );
        setTimeout(function() {
            tips.removeClass( "ui-state-highlight", 1500 );
        }, 500 );
    }

    function checkLengthPassword( o, n, min, max ) {
        if ( o.val().length > max || o.val().length < min ) {
            o.addClass( "ui-state-error" );
            updateTips( "Password length must be between " +
                min + " to " + max + "." );
            return false;
        } else {
            return true;
        }
    }
    function checkLengthRpPassword( o, n, min, max ) {
        if ( o.val().length > max || o.val().length < min ) {
            o.addClass( "ui-state-error" );
            updateTips( "Re-Type Password length must be between " +
                min + " to " + max + "." );
            return false;
        } else {
            return true;
        }
    }

    function checkComp(o) {

        if ( $("#password").val() != $("#repassword").val() ) {

            o.addClass( "ui-state-error" );
            updateTips( "Password and Re-Type Password are not same" );
            return false;
        } else {
            return true;
        }
    }
    function checkRegexp( o, regexp, n ) {
        if ( !( regexp.test( o.val() ) ) ) {
            o.addClass( "ui-state-error" );
            updateTips( n );
            return false;
        } else {
            return true;
        }
    }

    $( "#dialog-form" ).dialog({
        autoOpen: false,
        height: 300,
        width: 500,
        modal: true,
        buttons: {
            "Save": function() {
                var bValid = true;
                allFields.removeClass( "ui-state-error" );


                bValid = bValid && checkLengthPassword( password, "password", 4, 30 );
                bValid = bValid && checkLengthRpPassword( repassword, "repassword", 4, 30 );
                bValid = bValid && checkComp(password);
                // From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
                // bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );
                // bValid = bValid && checkRegexp( repassword, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );

                if ( bValid ) {

                    $( "#changepassword" ).submit();

                    //						$( "#dialog-form" ).append(  + password.val() +
                    //							 repassword.val()
                    //						 );
                    $( this ).dialog( "close" );
                }
            },
            Cancel: function() {
                $( this ).dialog( "close" );
            }
        },
        close: function() {
            allFields.val( "" ).removeClass( "ui-state-error" );
        }
    });

    $( "#passord_change" )
    .button()
    .click(function() {
        $( "#dialog-form" ).dialog( "open" );
    });

   
});




 